package com.talkweb.newproject.common.utils;

import javax.xml.crypto.dsig.XMLSignature;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**时间操作工具
 * Created by qiaosaifei on 2019/4/8.
 */
public class DateUtil {

    /**
     * 获取当前unix时间戳,毫秒值
     * @return
     */
    public static long getUnixTimestampMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前unix时间戳,秒数
     */
    public static long getUnixTimestamp() {
        return System.currentTimeMillis()/1000;
    }

    /**
     * 获取当前unix时间戳
     */
    public static String getUnixTimestampMillisString() {
        return String.valueOf(getUnixTimestampMillis());
    }

    /**
     * 获取当前时间
     */
    public static String getDateNow() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * 获取当前时间
     */
    public static String getDateShortNow() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     * 计算得到MongoDB存储的日期(默认情况下mongodb存储的是标准时间,中国时间是东八区,存在mongodb中少8小时,所以增加8小时,)
     * @return
     */
    public static Date getMongoDate() {
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.HOUR_OF_DAY,8);
        return ca.getTime();*/
        return new Date();
    }

    /**
     * 将字符串转换成日期
     */
    public static Date strToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date result = null;
        try {
             result = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将日期转换成字符串
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取天数偏差的日期
     */
    public static String getDiffDate(Integer diffDay) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,diffDay);
        date = cal.getTime();

        return sdf.format(date);
    }

    /**
     * 判断字符串是否是合法的日期类型
     * @param str
     * @return
     */
    public static boolean isValidDate(String str) {
        //指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm");
        boolean isValidDate=true;

        try {
            //设置lenient为false,否则SimpleDateFormat会比较宽松的验证日期,比如2007/02/29会被接受,并转换成2007/03/01
            sdf.setLenient(false);
            sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            isValidDate=false;
        }
        return isValidDate;
    }

    /**
     * 获取当前的起始时间
     */
    public static String getDiffStartDate(Integer diffDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,diffDay);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取当天的截止时间
     */
    public static String getDiffEndDate(Integer diffDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,diffDay);
        cal.set(Calendar.HOUR,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        cal.set(Calendar.MILLISECOND,59);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取天数偏差的日期
     */
    public static String getDiffDateDay(Integer diffDay) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, diffDay);
        date = calendar.getTime();
        String strDate= sdf.format(date);
        return strDate;
    }

    public static Date getNextDay(Date date) {
        Calendar   calendar   =   new GregorianCalendar();
        calendar.setTime(date);
//        calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
//        calendar.add(calendar.DAY_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
//        calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }
}
